<?php

namespace common\logic\sms;

use common\tool\HttpCurl;

class TplSingleSend implements SendMsgInterface{
    const NAME = "tpl_sign";
    
    const APIKEY = 'apikey';
    const MOBILE = 'mobile';
    const TEXT = 'text';
    
    protected $must = [];
    public function __construct(){
        $this->must = [self::APIKEY,self::MOBILE,self::TEXT];
    }
    
    /**指定模版单发
     * @param apikey成功注册后登录云片官网,进入后台可查看
     * @param mobile接收的手机号,仅支持单号码发送
     * @param tpl_id发送的模版id
     * @param tpl_value模版内容，变量名和变量值对,如果内容含有％注意需要是中文全角的
     * @return json格式字符串
     */
    public function send(array $params){
        $r = SmsCommon::verifyParam($params, $this->must);
        if($r){
            return $r;
        }
        
        $res = HttpCurl::curlRequest(Configs::smsurl.Configs::tpl_single_send_https, $params);
        $res = json_decode($res, true);
        
        if($res === false || 0 != $res['code']) {
            $this->sendFail($params, $res);
        }
        $this->sendSuccess($params, $res);
    }
    //发送成功
    public function sendSuccess(array $sendData, array $sendRes){
    
    }
    //发送失败
    public function sendFail(array $sendData, array $sendRes){
    
    }
    //发送日志
    public function sendLog(array $sendData, array $sendRes){
        //初始化日志
     
    }
    
    public function getTemplateValue($values)
    {
        $arr = '';
        foreach ($values as $key => $value) {
            $arr[] = urlencode('#' . $key . '#') . '=' . urlencode($value);
        }
        return implode('&', $arr);
    }
  
}